<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Organization\Repository;

use Plugin\Organization\Model\RdOrganizeUser;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 工区账号公司关联表 Repository类
 */
class RdOrganizeUserRepository extends IRepository
{
   public function __construct(
        protected readonly RdOrganizeUser $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 企业id
        if (isset($params['enterprise_id']) && filled($params['enterprise_id'])) {
            $query->where('enterprise_id', '=', $params['enterprise_id']);
        }

        // 微信openid
        if (isset($params['wechat_openid']) && filled($params['wechat_openid'])) {
            $query->where('wechat_openid', '=', $params['wechat_openid']);
        }

        // 部门
        if (isset($params['department']) && filled($params['department'])) {
            $query->where('department', 'like', '%' . $params['department'] . '%');
        }

        // 职位
        if (isset($params['position']) && filled($params['position'])) {
            $query->where('position', 'like', '%' . $params['position'] . '%');
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 创建者
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        // 更新者
        if (isset($params['updated_by']) && filled($params['updated_by'])) {
            $query->where('updated_by', '=', $params['updated_by']);
        }

        // 备注
        if (isset($params['remark']) && filled($params['remark'])) {
            $query->where('remark', 'like', '%'.$params['remark'].'%');
        }

        return $query;
    }

    protected function enablePageOrderBy(): bool
    {
        return false;
    }

    public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
    {
        $result = $this->perQuery($this->getQuery(), $params)
            ->with(['enterprise:id,name,icon', 'user:id,username,avatar,email,status,user_type,phone,login_ip,login_time,remark,created_at,updated_at,nickname,signed,backend_setting'])
            ->paginate(
            perPage: $pageSize,
            pageName: static::PER_PAGE_PARAM_NAME,
            page: $page,
        );
        return $this->handlePage($result);
    }

    public function findByOpenId(mixed $openId)
    {
        return $this->getQuery()
            ->where('wechat_openid' ,'=', $openId)
            ->first();
    }
}